$(function() {
	var projectGrid = $("#projectGrid");
	var xmGridAction = $("#xmGridAction");
	// 初始
	projectGrid.datagrid({
		url : 'project/list',
		fit : true,
		rownumbers : true,
		pagination : true,
		fitColumns : true,// 自适应大小
		columns : [ [
				{
					field : 'name',
					title : '创建人',
					align : "center",
					width : 80
				},
				{
					field : 'project_type',
					title : '项目类型',
					align : "center",
					width : 130,
					formatter : function(value, row, index) {
						if (value != null) {
							return value + "项目"
						} else {
							return "暂不详细";
						}
					}
				},
				{
					field : 'TeamCount',
					title : '团队数量',
					align : "center",
					width : 80
				},
				{
					field : 'create_time',
					title : '创建时间',
					width : 120,
					align : "center",
					formatter : function(value, row, index) {
						var c = new Date(value);// 传入一个时间戳
						c.toLocaleString();// 返回本地时间
						var d = new Date(c);
						var formatIt = d.getFullYear() + '-'
								+ (d.getMonth() + 1) + '-' + d.getDate() + ' '
								+ d.getHours() + ':' + d.getMinutes() + ':'
								+ d.getSeconds();
						return formatIt;

					}
				},
				{
					field : 'ac',
					title : '操作',
					align : 'center',
					width : 100,
					formatter : function(value, row, index) {
						/*var bts = [];
						bts.push('<a data-index="' + index
								+ '" class="ctr edit">编辑</a>');
						bts.push('<a data-id="' + row.id + '" data-index="'
								+ index + '" class="ctr delete">删除</a>');*/
						return xmGridAction.children("a.ctr").attr('data-id',
								row.id).attr('data-index', index)
								.end().html();
					}
				} ] ],
		toolbar : [ {
			iconCls : 'icon-add',
			text : "添加项目",
			handler : function() {
				Addproject();
			}
		} ]
	});
	var userName = $("#userName");
	var projectType = $("#projectType")
	$("#projectSearch").on('click', function() {
		projectGrid.datagrid("load", {
			projectData : "%" + userName.val() + "%",
			projectType : projectType.val()
		});
	});

	var projectForm = $("#projectForm")
	$("#projectReset").on('click', function() {
		projectForm.form("clear");
		projectGrid.datagrid("load", {});
	});

	var gridPanel = projectGrid.datagrid("getPanel");
	gridPanel.on("click", "a.edit", function() {
		var index = this.dataset.index;
		var rows = projectGrid.datagrid("getRows");
		var id = rows[index].project_id;
		Addproject(id);
	}).on("click", "a.delete", function() {
		var index = this.dataset.index;
		var rows = projectGrid.datagrid("getRows");
		var id = rows[index].project_id;
		$.messager.confirm("提示", "你确定要删除此数据？", function(r) {
			if (r) {
				$.get("project/delete/" + id, function(rs) {
					if (rs.success) {
						projectGrid.datagrid("reload");
					}else{
						$.message.alert("提示",rs.message)
					}
				});
			}
		});
	});
	function Addproject(id) {
		var dialog = $("<div/>").dialog({
			href : 'jiaoxue/jx-add-project' + (id ? ("/" + id) : ""),
			title : "添加项目",
			height : 250,
			width : 340,
			modal : true,
			onClose : function() {
				// 窗口关闭事销毁此窗口
				$(this).dialog("destroy");
			},
			buttons : [ {
				iconCls : 'icon-save',
				text : '保存',
				handler : function() {
					var addProjectForm = $("#addProjectForm");
					if (addProjectForm.form('validate')) {
						$.post("project/save", addProjectForm.serialize(), function(rs) {
							if (rs.success) {
								projectGrid.datagrid("reload");
								dialog.dialog("close");
							} else {
								$.messager.alert("提示", rs.message);
							}
						});
					}
				}
			}, {
				iconCls : 'icon-clear',
				text : '取消',
				handler : function() {
					dialog.dialog("close");
				}
			} ]
		});
	}
});